package com.zf.shuashua.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.animator.ScaleAlphaAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Constants;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.ui.gift.H5Activity;
import com.zf.shuashua.ui.popup.OnPopupListener;

import static com.lxj.xpopup.enums.PopupAnimation.ScaleAlphaFromCenter;
import static com.lxj.xpopup.enums.PopupAnimation.NoAnimation;

public class PrivacyPopup extends CenterPopupView {

    private Activity context;
    private TextView tvContent;
    private TextView tvConfirm;
    private TextView tvtoPolicy;
    private TextView tvtoProtocol;
    private TextView tvTitle1;
    private TextView tvCancel;
    private OnPopupListener mListener;

    public PrivacyPopup(@NonNull Activity context) {
        super(context);
        this.context = context;
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.privacy_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        String s1 = "　　1.在仅浏览时,为保障服务所需,我们会申请系统权限收集设备信息、日志信息,用于信息推送和安全风控,并申请存储权限,用于下载短视频及缓存相关文件。\n" +
                "　　2.为了基于你的所在位置向你推荐内容、在你的个人主页显示位置信息,或经你选择在你发布的视频中显示位置信息,我们可能会申请位置权限。\n" +
                "　　3.为了帮你发现更多好友,我们可能会申请通讯录权限。\n" +
                "　　4.我们尊重你的选择权,如果你不希望被推荐给好友,你可以在“我设置-隐私设置”中随时关闭。\n" +
                "　　5.为帮助你在APP中拨打投诉电话或其他咨询热线,我们可能会申请拨打电话权限,该权限不会收集隐私信息,且仅在你使用前述功能时使用。\n" +
                "　　6.通讯录、GPS、 摄像头、麦克风、相册等敏感权限均不会默认开启,只有经过明示授权才会为实现功能或服务时使用,你均可以拒绝且不影响你继续使用长豆。";
        tvContent = findViewById(R.id.tvContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvConfirm = findViewById(R.id.tvConfirm);
        tvtoPolicy = findViewById(R.id.tvtoPolicy);
        tvtoProtocol = findViewById(R.id.tvtoProtocol);
        tvTitle1 = findViewById(R.id.tvTitle1);
        tvContent.setText(s1);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());

        tvTitle1.setText(getClickableSpan());
        //设置该句使文本的超连接起作用
        tvTitle1.setMovementMethod(LinkMovementMethod.getInstance());

        tvConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Store.getInstance().setPrivacy(true);
                if (mListener != null) {
                    mListener.onNext();
                }
                dismiss();
            }
        });
        tvtoPolicy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, H5Activity.class).putExtra("money", Constants.H5_URL + "policy").putExtra("title", "长豆隐私政策"));
            }
        });

        tvtoProtocol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, H5Activity.class).putExtra("money", Constants.H5_URL + "protocol").putExtra("title", "长豆用户协议"));

            }
        });


        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.finish();
            }
        });


    }

    //设置超链接文字
    private SpannableString getClickableSpan() {
//        SpannableString spanStr = new SpannableString("使用该软件，即表示您同意该软件的使用条款和隐私政策");
        SpannableString spanStr = new SpannableString("为了让您更好地使用长豆，请充分阅读并理解《用户服务协议》和《隐私政策协议》");
        //设置下划线文字
        spanStr.setSpan(new UnderlineSpan(), 20, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                context.startActivity(new Intent(context, H5Activity.class).putExtra("money", Constants.H5_URL + "protocol").putExtra("title", "长豆用户协议"));

            }
        }, 20, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(Color.parseColor("#55AEE2")), 20, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置下划线文字
        spanStr.setSpan(new UnderlineSpan(), 29, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                context.startActivity(new Intent(context, H5Activity.class).putExtra("money", Constants.H5_URL + "policy").putExtra("title", "长豆隐私政策"));

            }
        }, 29, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(Color.parseColor("#55AEE2")), 29, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanStr;
    }

    @Override
    protected PopupAnimator getPopupAnimator() {
        return new ScaleAlphaAnimator(getPopupContentView(), NoAnimation);
    }
}

